const path = require('path')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { DefinePlugin } = require('webpack')
const CopyWebpackPlugin = require('copy-webpack-plugin')

module.exports = {
    mode: "development",
    entry: "./src/main.js",
    output: {
        filename: "bundle.js",
        path: path.resolve(__dirname, './build')
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    { loader: "style-loader" },
                    { loader: "css-loader" },
                    // postcss-loader 中的配置可以另外写在 postcss.config.js 文件中
                    // {
                    //     loader: "postcss-loader",
                    //     options: {
                    //         postcssOptions: {
                    //             plugins: [
                    //                 require("postcss-preset-env")
                    //             ]
                    //         }
                    //     }
                    // }
                ]
            },
            {
                // 查看官方文档得知 在limit大小范围内的图片会使用url-loader去解析
                // 但是超过limit大小的图片 会默认启动file-loader去解析
                test:  /\.(png|jpg|gif)$/i,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 100*1024,
                            name: "img/img---[name].[hash:8].[ext]"
                        }
                    }
                ]
            }
        ],
    },
    plugins: [
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({
            title: 'webpack study',
            template: './public/index.html'
        }),
        new DefinePlugin({
            BASE_URL: '"./"',
            TEXT: '"haha"'
        }),
        new CopyWebpackPlugin({
            patterns: [
                {
                    from: 'public',
                    globOptions: {
                        ignore: [
                            "**/index.html",
                            "**/.DS_Store",
                            "**/abc.txt",
                            "**/index.html"
                          ]
                    }
                }
            ]
        })
    ]
}